<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\facade\Env;
use app\admin\model\Pic as PicModel;
use app\admin\validate\Pic as PicValidate;

class Pic extends Controller
{
    //显示照片列表
    public function index($page = 1, $q = '')
    {
        $q = input('get.q');
        $pageParam = ['query' => []];
        if($q){
            $pageParam['query']['q'] = $q;
            $list = PicModel::with('admin')->where('pic_info', 'LIKE', "%$q%")->order('pic_id desc')->paginate(6,false,$pageParam);
        }else{
            $list = PicModel::with('admin')->order('pic_id desc')->paginate(6);
        }

        //$list = PicModel::with('admin')->order('pic_id desc')->paginate(4);
        $picCount = PicModel::count();
        $this->assign('picCount', $picCount);
        $this->assign('list', $list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }

    //所有人都能查照片列表
    public function all($page = 1, $q = '', $date='')
    {
        $q = input('get.q');
        $date = input('get.date');
        $pageParam = ['query' => []];
        $where[] = ['admin_role', '=', 103];
        $where_date = null;

        $list = PicModel::hasWhere('admin', $where)->order('pic_id desc')->paginate(6);

        if($q){
            $where[] = ['tel|admin_name', 'LIKE', "%$q%"];
            $pageParam['query']['q'] = $q;
            $list = PicModel::hasWhere('admin', $where)->order('pic_id desc')->paginate(6,false,$pageParam);
        }

        if($date){
            $where_date[] = ['pic_time', '>=', strtotime($date)];
            $where_date[] = ['pic_time', '<=', strtotime($date) + 86400];
            $pageParam['query']['date'] = $date;
            $list = PicModel::hasWhere('admin', $where)->where($where_date)->order('pic_id desc')->paginate(6,false,$pageParam);
            //$list = PicModel::hasWhere('admin', $where)->where('pic_info', 'LIKE', "%$q%")->order('pic_id desc')->paginate(6,false,$pageParam);
        }

        //$list = PicModel::with('admin')->order('pic_id desc')->paginate(4);
        $picCount = PicModel::hasWhere('admin', ['admin_role'=> 103])->count();
        $this->assign('picCount', $picCount);
        $this->assign('list', $list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        $this->assign('date', $date);
        return $this->fetch();
    }

    //根据用户id(admin_id)获得照片的列表
    public function self($page = 1, $q = '')
    {
        $q = input('get.q');
        $pageParam = ['query' => []];

        $admin_id = session('id');

        if($q){
            $pageParam['query']['q'] = $q;
            $list = PicModel::with('admin')->where('admin_id', '=', $admin_id)->where('pic_info', 'LIKE', "%$q%")->order('pic_id desc')->paginate(6,false,$pageParam);
        }else{
            $list = PicModel::with('admin')->where('admin_id', '=', $admin_id)->order('pic_id desc')->paginate(6);
        }

        //$list = PicModel::with('admin')->order('pic_id desc')->paginate(4);
        $picCount = PicModel::where('admin_id', '=', $admin_id)->count();
        $this->assign('picCount', $picCount);
        $this->assign('list', $list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }

    public function add(Request $request)
    {
        if($request->isPost()){
            //dump($request->param());die;
            $validate = new PicValidate;
            //验证,pic_info,admin_id,__token__
            if(!$validate->check($request->param())){
                $this->error($validate->getError());
            }
            //照片上传
            $file = $request->file('picture');
            // 上传照片验证
            $result = $this->validate(['file' => $file], ['file'=>'require|image'],['file.require' => '请选择照片文件', 'file.image' => '非法图像文件']);
            if(true !== $result){
                $this->error($result);
            }
            //移动到框架应用根目录/public/uploads/目录下
            $info = $file->rule('date')->move(Env::get('root_path') . 'public' . '/' . 'uploads');
            if($info){
                //$this->success('文件上传成功：' . $info->getRealPath());
                $imgPath ='/uploads/' . $info->getSaveName();
                //dump($info->getSaveName());
                //dump($info->getFilename());
                //生成缩略图
                $image = \think\Image::open('.' . $imgPath);
                //$image->thumb(1000, 1000)->save('./uploads/20180626/xxx.jpg');
                $image->thumb(600, 600)->save('.' . $imgPath);
                //写入数据库
                $pic = new PicModel;
                $pic->pic_info = $request->param('pic_info');
                $pic->pic_url = $imgPath;
                $pic->admin_id = $request->param('admin_id');
                $pic->save();
                $this->redirect('admin/Sign/info');
            }else{
                //上传失败获取错误信息
                $this->error($file->getError());
            }
        }
        //前台存admin_id
        $this->assign('id', session('id'));
        return $this->fetch();
    }

    public function del($pic_id = 0)
    {
        $pic = PicModel::get($pic_id);
        if($pic)
        {
            $this->assign('pic',$pic);
        } else {
            echo '非法参数！';die;
        }

        if($this->request->isPost()){
            PicModel::destroy($pic_id);
            return redirect('admin/Pic/index');
        }
        return $this->fetch();
    }

    public function export(Request $request)
    {
        //dump($request->domain());die;
        if(request()->isPost())
        {
            $begin_date = $request->param('begin_date');
            $end_date = $request->param('end_date');
            $tel = $request->param('tel');
            $where = null;
            if($begin_date){
                $where[] = ['pic_time', '>=', strtotime($begin_date)];
            }

            if($end_date){
                $where[] = ['pic_time', '<=', strtotime($end_date)];
            }

            //导出数据
            // excel表头
            $headArr[] = '照片说明';
            $headArr[] = '上传时间';
            $headArr[] = '上传者';
            $headArr[] = '照片路径';

            // 数据
            if($where != null) {
                $list = PicModel::with('admin')->where($where)->select();
            }else{
                $list = PicModel::with('admin')->select();
            }

            $data = array();
            foreach($list as $pic){
                // 获取学生选课数据
                $datatemp[0]=$pic->pic_info;
                $datatemp[1]=$pic->pic_time;
                //$datatemp[2]=$pic->admin->admin_name ?? '';
                $datatemp[2]=isset($pic->admin->admin_name) ? $pic->admin->admin_name : '';
                $datatemp[3]=$request->domain() . $pic->pic_url;

                if($tel){
                    if($tel == $pic->admin->tel){
                        $data[] = $datatemp;
                    }
                }else{
                    $data[] = $datatemp;
                }
                //$data[] = $datatemp;
            }
            //dump($data);die;
            $filename = '照片信息表';
            if($data){
                export_excel($filename, $headArr, $data); // $filename excel名称 $headArr excel表头
            }
        }
        return $this->fetch();
    }

}
